package com.wontube.core.conf;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.google.gson.Gson;
import com.wontube.client.extension.utils.FileUtils;

/**
 * @author yuechao
 *
 * 代理设置
 * 
 */
public final class Proxy implements Serializable {
	private static final long serialVersionUID = 4490491052404302977L;
	private static final Logger logger = LoggerFactory.getLogger(Proxy.class);
	
	private boolean isUseProxy;
	private String proxyAddress;
	private int proxyPort;
	
	public static final Proxy defaultProxy = new Proxy();
	
	private Proxy() {
	}
	
	/**
	 * @return the isUseProxy
	 */
	public boolean isUseProxy() {
		return this.isUseProxy;
	}
	/**
	 * @param isUseProxy the isUseProxy to set
	 */
	public void setUseProxy(boolean isUseProxy) {
		this.isUseProxy = isUseProxy;
	}
	/**
	 * @return the proxyAddress
	 */
	public String getProxyAddress() {
		return this.proxyAddress;
	}
	/**
	 * @param proxyAddress the proxyAddress to set
	 */
	public void setProxyAddress(String proxyAddress) {
		this.proxyAddress = proxyAddress;
	}
	
	/**
	 * @return　the proxyPort
	 */
	public int getProxyPort() {
		return this.proxyPort;
	}
	
	/**
	 * @param proxyPort
	 */
	public void setProxyPort(int proxyPort) {
		this.proxyPort = proxyPort;
	}

	public static Proxy load(File file) {
		logger.info("begen to load proxy settings");
		
		String json = "";
		try {
			json = FileUtils.readAllText(file);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		
		if ("".equals(json)) {
			return new Proxy();
		}
	
		Gson gson = new Gson();
		Proxy proxy = gson.fromJson(json, Proxy.class);
		defaultProxy.setProxyAddress(proxy.getProxyAddress());
		defaultProxy.setProxyPort(proxy.getProxyPort());
		defaultProxy.setUseProxy(proxy.isUseProxy());
		
		logger.info("finished loading proxy settings, isUseProxy = {}", proxy.isUseProxy);
		
		return proxy;
	}
}
